package com.pzl.controller;

import com.pzl.entity.Role;
import com.pzl.entity.User;
import com.pzl.service.RoleService;
import com.pzl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.sound.midi.SoundbankResource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * UserController class
 *
 * @Author panzhenglei
 * @Date 2019/6/8 6:44 PM
 */


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;


    @RequestMapping("/list")
    public ModelAndView list() throws Exception {

        ModelAndView modelAndView = new ModelAndView();
        List<User> users = userService.findAll();

        modelAndView.addObject("userList",users);


        modelAndView.setViewName("user-list");

        return modelAndView;

    }


    @RequestMapping("/save")
    public ModelAndView save(Long id, String username, String email, String password, String phoneNum, Long[] roleIds) throws Exception {

        ModelAndView modelAndView = new ModelAndView();



        System.out.println("0000000");
        User user = new User();
        //user.setRoles(roleIds);
        user.setEmail(email);
        user.setId(id);
        user.setPassword(password);
        user.setPhoneNum(phoneNum);
        user.setUsername(username);
        System.out.println("-----");
        userService.save(user,roleIds);
        List<User> users = userService.findAll();
        System.out.println("1111");



        modelAndView.addObject("userList",users);

        modelAndView.setViewName("user-list");

        return modelAndView;

    }

    @RequestMapping("saveUI")
    public ModelAndView saveUI(){

        ModelAndView modelAndView = new ModelAndView();


        List<Role> role = roleService.findAll();
        modelAndView.addObject("role",role);


        modelAndView.setViewName("user-add");

        return modelAndView;
    }


    @RequestMapping("updateUI")
    public ModelAndView update(Long id) throws Exception {
        ModelAndView modelAndView = new ModelAndView();

        User one = userService.findOne(id);
        //System.out.println(one);
        List<Role> role = roleService.findAll();

        //modelAndView.addObject("role",role);
        List<Role> roles = one.getRoles();
        //System.out.println(roles);
        HashMap<Role, String> roleIntegerHashMap = new HashMap<>();

        for (Role role1 : role) {
            roleIntegerHashMap.put(role1,"0");
            for (Role role2 : roles) {
                if (role2.equals(role1)){
                    roleIntegerHashMap.put(role1,"1");
                    break;
                }
            }
        }
        //modelAndView.addObject("drole",roles);

        modelAndView.addObject("role",roleIntegerHashMap);
        System.out.println(roleIntegerHashMap);
        modelAndView.addObject("user",one);

        modelAndView.setViewName("user-update");

        return modelAndView;

    }

    @RequestMapping("/update")
    public ModelAndView update(Long id, String username, String email, String password, String phoneNum, Long[] roleIds) throws Exception {

        ModelAndView modelAndView = new ModelAndView();

        User user = new User();
        user.setEmail(email);
        user.setId(id);
        user.setPassword(password);
        user.setPhoneNum(phoneNum);
        user.setUsername(username);

        userService.update(user,roleIds);

        List<User> users = userService.findAll();

        modelAndView.addObject("userList",users);

        modelAndView.setViewName("user-list");

        return modelAndView;

    }
}
